import { Guid } from "guid-typescript";
import store from "@/store";
import { LandingAgency, Partners, Project } from "@/interface/entity";
import { constObjs } from "@/const/const";
import {
  myDateFormat,
  percentageToNumber,
  thousandsToNumber
} from "@/functions/general";
import { ElMessage } from "element-plus";

// before add/update/delete project to db
export const processProjectBeforeToDb = (
  project: Project,
  method = ""
): Project => {
  const projectForm = JSON.parse(JSON.stringify(project)) as Project;

  switch (method) {
    case "add": {
      // 落地机构和合作方的guid和项目一样
      // 创建guid
      projectForm.projectGuid = Guid.create().toString();

      // 创建项目的账号
      projectForm.creator = store.state.username;

      // 已到账金额和未到账金额
      projectForm.unpaidAmount = projectForm.projectFund;
      break;
    }
    default:
      break;
  }

  // 生成所属地
  // todo to string in db
  projectForm.selectedArea = (projectForm.selectedArea as string[]).join(" ");

  // 格式化日期
  projectForm.createdDate = myDateFormat(projectForm.createdDate);
  projectForm.trainingStartTime = myDateFormat(projectForm.trainingStartTime);
  projectForm.trainingEndTime = myDateFormat(projectForm.trainingEndTime);
  projectForm.contractStartTime = myDateFormat(projectForm.contractStartTime);
  projectForm.contractEndTime = myDateFormat(projectForm.contractEndTime);
  // to json string
  // todo client
  projectForm.clients = JSON.stringify(projectForm.clients);
  projectForm.landingAgencies = JSON.stringify(project.landingAgencies);
  projectForm.partners = JSON.stringify(project.partners);

  return projectForm;
};

// generate the projects to show in the table
export const generateProjectLists = (projects: Project[]): Project[] => {
  // deep copy
  const projectTableData = JSON.parse(JSON.stringify(projects)) as Project[];
  //
  let maxLandingAgenciesIndex = 0,
    maxPartnersIndex = 0,
    maxLandingAgenciesNum = 0,
    maxPartnersNum = 0,
    maxClietnsIndex = 0,
    maxClietnsNum = 0;

  // loop
  projectTableData.forEach((project, index) => {
    // 把所属地变为数组形式
    if (
      project.landingAgencies == undefined ||
      project.landingAgencies == null
    ) {
      ElMessage.error(project.projectName);
    }
    // convert to js object when it is a JSON string
    // todo client
    project.clients = JSON.parse(project.clients as string) as LandingAgency[];
    project.landingAgencies = JSON.parse(
      project.landingAgencies as string
    ) as LandingAgency[];
    project.partners = JSON.parse(project.partners as string) as Partners[];

    // update the max
    // todo client
    if (project.clients.length > maxClietnsNum) {
      maxClietnsNum = project.landingAgencies.length;
      maxClietnsIndex = index;
    }
    if (project.landingAgencies.length > maxLandingAgenciesNum) {
      maxLandingAgenciesNum = project.landingAgencies.length;
      maxLandingAgenciesIndex = index;
    }
    if (project.partners.length > maxPartnersNum) {
      maxPartnersNum = project.partners.length;
      maxPartnersIndex = index;
    }

    //
    project.profitlossAmount =
      (project.paidAmount as number) - (project.outlayAmount as number);
  });
  // save the max
  constObjs.projectsTablePattern.forEach(pattern => {
    if (pattern.prop == "clients") {
      pattern.maxLengthIndex = maxClietnsIndex;
      pattern.maxSubItemNum = maxClietnsNum;
    }
    if (pattern.prop == "landingAgencies") {
      pattern.maxLengthIndex = maxLandingAgenciesIndex;
      pattern.maxSubItemNum = maxLandingAgenciesNum;
    }
    if (pattern.prop == "partners") {
      pattern.maxLengthIndex = maxPartnersIndex;
      pattern.maxSubItemNum = maxPartnersNum;
    }
  });
  return projectTableData;
};

// when uploading project

export const processUploadedProject = (projects: Project[]): Project[] => {
  projects.forEach(project => {
    Object.keys(project).forEach(key => {
      // 日期
      if (key.includes("Date")) {
        (project[key as keyof Project] as string) = myDateFormat(
          project[key as keyof Project] as string
        );
      }
      // 金额
      else if (
        key.includes("Amount") ||
        key == "classPayAmount" ||
        key == "welfareAmount" ||
        key == "devFundingAmount" ||
        key == "schoolManageAmount" ||
        key == "projectFund" ||
        key == "outlayAmount"
      ) {
        (project[key as keyof Project] as number) = thousandsToNumber(
          project[key as keyof Project] as string
        );
      }
      // 比例
      else if (key.includes("Radio")) {
        (project[key as keyof Project] as number) = percentageToNumber(
          project[key as keyof Project] as string
        );
      }
    });

    // todo 委托方，落地机构，合作方
    (project.clients as LandingAgency[]).forEach(agency => {
      agency.shareAmount = thousandsToNumber(agency.shareAmount.toString());
      agency.shareRadio = percentageToNumber(agency.shareRadio.toString());
    });
    (project.landingAgencies as LandingAgency[]).forEach(agency => {
      agency.shareAmount = thousandsToNumber(agency.shareAmount.toString());
      agency.shareRadio = percentageToNumber(agency.shareRadio.toString());
    });

    (project.partners as Partners[]).forEach(partner => {
      partner.shareAmount = thousandsToNumber(partner.shareAmount.toString());
      partner.shareRadio = percentageToNumber(partner.shareRadio.toString());
    });

    project.landingAgencies = JSON.stringify(project.landingAgencies);
    project.partners = JSON.stringify(project.partners);

    // 主键
    project.projectGuid = Guid.create().toString();
  });

  return projects;
};
